/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConfirmCostFrame.java
 *
 * Created on Jan 18, 2010, 9:08:38 PM
 */

package org.acmwustl.bubble.gui;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.SwingWorker;
import org.acmwustl.bubble.controllers.PaymentController;
import org.acmwustl.bubble.gui.dialogs.NotAMemberDialog;
import org.acmwustl.bubble.logic.ConnectionManager;
import org.acmwustl.bubble.util.PriceToString;
import org.acmwustl.bubble.windowutils.WindowUtils;

/**
 *
 * @author officer
 */
public class ConfirmPaymentFrame extends javax.swing.JFrame
implements KeyListener {

    private final PaymentController myController;
    private final int amountInCents;
    private String keyIDCode = "";

    private boolean ignoreInput = false;

    /** Creates new form ConfirmCostFrame */
    public ConfirmPaymentFrame(int cost, PaymentController pc) {
        initComponents();
        myController = pc;
        amountInCents = cost;

        setSize(new Dimension(1024,768));
        WindowUtils.centerWindow(this);

        costLabel.setText(PriceToString.getPriceAsString(amountInCents));

        this.getRootPane().addKeyListener(this);
        this.getRootPane().requestFocus();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        costLabel = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setUndecorated(true);

        jButton1.setText("Cancel Payment");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        costLabel.setFont(new java.awt.Font("Dialog", 1, 65));
        costLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        costLabel.setText("$0.00");

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("Please swipe your card to complete your payment, or cancel and return to the previous screen.");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 616, Short.MAX_VALUE)
                    .addComponent(costLabel, javax.swing.GroupLayout.Alignment.CENTER, javax.swing.GroupLayout.DEFAULT_SIZE, 616, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 616, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(costLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(143, 143, 143))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel costLabel;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

    public void keyTyped(KeyEvent e) {
        if(!ignoreInput) {
            keyIDCode += e.getKeyChar();
            if(keyIDCode.length() == 19) {
                if(keyIDCode.matches(";\\d{5}=\\d{11}\\?")) {
                    dispose();
                    if(ConnectionManager.getInstance().isRegistered(keyIDCode))
                        myController.confirmPayment(this, keyIDCode, amountInCents);
                    else
                        new NotAMemberDialog(this, true).setVisible(true);
                } else {
                    keyIDCode = "";
                    new IgnoreInputTimer().execute();
                    Toolkit.getDefaultToolkit().beep();
                }
            }
        }
    }

    public void keyPressed(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {}

    private class IgnoreInputTimer extends SwingWorker {

        private long time = 300;
        private boolean running = true;
        private long lastMillis;

        public IgnoreInputTimer() {
            ignoreInput = true;
        }

        @Override
        protected Object doInBackground() throws Exception {
            lastMillis = System.currentTimeMillis();
            while(running) {
                time -= (System.currentTimeMillis() - lastMillis);
                lastMillis = System.currentTimeMillis();
                if(time <= 0) {
                    ignoreInput = false;
                    running = false;
                    System.err.println("DONE!");
                }
                else
                    Thread.sleep(100);
            }
            return null;
        }
    }
}
